package com.yy.jhl.util.greendao;

import android.content.Context;
import android.util.Log;

import com.yy.jhl.bean.ExceptionModel;
import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

/**
 * 完成对某一张数据表的具体操作，ORM操作
 * Created by Mr.sorrow on 2017/5/5.
 */

public class ExceptionDaoUtils {
    private static final String TAG = ExceptionDaoUtils.class.getSimpleName();
    private DaoManager mManager;
    private static ExceptionDaoUtils instance;
    public static ExceptionDaoUtils getInstance(Context context){
        if (instance == null){
            synchronized (ExceptionDaoUtils.class){
                instance = new ExceptionDaoUtils(context);
            }
        }
        return instance;
    }
    public ExceptionDaoUtils(Context context) {
        mManager = DaoManager.getInstance();
        mManager.init(context);
    }

    /**
     * 完成插入，如果表未创建，先创建表
     *
     * @param exceptionModel
     * @return
     */
    public boolean insertExceptionModel(ExceptionModel exceptionModel) {
        boolean flag = false;
        deleteExceptionModel(exceptionModel);
        flag = mManager.getDaoSession().getExceptionModelDao().insert(exceptionModel) == -1 ? false : true;
        Log.i(TAG, "insert purchaseModel :" + flag + "-->" + exceptionModel.toString());

        return flag;
    }

    /**
     * 插入多条数据，在子线程操作
     * @param exceptionModelList
     * @return
     */
    public boolean insertExceptionModel(final List<ExceptionModel> exceptionModelList) {
        boolean flag = false;
        try {
            mManager.getDaoSession().runInTx(new Runnable() {
                @Override
                public void run() {
                    for (ExceptionModel meizi : exceptionModelList) {
                        mManager.getDaoSession().insertOrReplace(meizi);
                    }
                }
            });
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 修改一条数据
     * @param exceptionModel
     * @return
     */
    public boolean updateExceptionModel(ExceptionModel exceptionModel){
        boolean flag = false;
        try {
            mManager.getDaoSession().update(exceptionModel);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除单条记录
     * @param exceptionModel
     * @return
     */
    public boolean deleteExceptionModel(ExceptionModel exceptionModel){
        boolean flag = false;
        try {
            //按照id删除
            mManager.getDaoSession().delete(exceptionModel);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除所有记录
     * @return
     */
    public boolean deleteAll(){
        boolean flag = false;
        try {
            mManager.getDaoSession().deleteAll(ExceptionModel.class);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 查询所有记录
     * @return
     */
    public List<ExceptionModel> queryAllExceptionModel(){
        return mManager.getDaoSession().loadAll(ExceptionModel.class);
    }

    /**
     * 根据主键id查询记录
     * @param key
     * @return
     */
    public ExceptionModel queryExceptionModelById(long key){
        return mManager.getDaoSession().load(ExceptionModel.class, key);
    }

    /**
     * 使用native sql进行查询操作
     */
    public List<ExceptionModel> queryExceptionModelByNativeSql(String sql, String[] conditions){
        return mManager.getDaoSession().queryRaw(ExceptionModel.class, sql, conditions);
    }

    /**
     * 分页进行查询操作
     */
    public List<ExceptionModel> queryExceptionModelByOffset(int offset){
        QueryBuilder<ExceptionModel> queryBuilder = mManager.getDaoSession().queryBuilder(ExceptionModel.class).limit(10).offset(offset * 10);
        return queryBuilder.list();
    }

    /**
     * 使用queryBuilder进行查询
     * @return
     */
    public List<ExceptionModel> queryExceptionModelByQueryBuilder(long id){
        QueryBuilder<ExceptionModel> queryBuilder = mManager.getDaoSession().queryBuilder(ExceptionModel.class);
        return queryBuilder.where(ExceptionModelDao.Properties.Id.eq(id)).list();
    }
}
